package m.jclecai.com.pay;

import android.text.TextUtils;

import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.bean.PayChannelBean;
import m.jclecai.com.utils.http.HttpManager;

/**
 * 支付宝充值
 * Created by zed on 2018/3/12.
 */

public class RedirectPayImpl extends BasePayImpl {

    public RedirectPayImpl(BaseActivity activity) {
        super(activity);
    }

    @Override
    public void pay(PayChannelBean channelBean, String payMoney) {
        setPaying(false);
        showDialog();
        getRedirectOrder(channelBean);
    }

    //channel包含redirect
    private void getRedirectOrder(final PayChannelBean payChannelBean) {
        Map<String, String> params = new HashMap<>();
        params.put("body", "Account");
        params.put("total_fee", payChannelBean.getAmount() + "");
        params.put("out_trade_no", payChannelBean.getProjectno());
        setOrderNumber(payChannelBean.getProjectno());
        HttpManager.postAsyn("payment/" + payChannelBean.getKey() + "/order.php", new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                dissDialog();
                DisPlay("充值失败");
            }

            @Override
            public void onResponse(String response) {
                dissDialog();
//                weakUpAPPPay(payChannelBean.getKey(), response, payChannelBean.getName());
                if(!TextUtils.isEmpty(response)){
                    if (response.contains("404")){
                        DisPlay("404");
                    }else{
                        weakUpPay(payChannelBean.getKey(), response);
                    }
                }else{
                    DisPlay("充值失败");
                }

            }
        }, params);
    }

}
